/**
 * 
 */
package kr.co.insoft.core.repository.configration.cdt.impl;

import kr.co.insoft.core.mapper.configration.cdt.CDTMetadataEntityMapper;
import kr.co.insoft.core.model.configration.cdt.CDTEntityModel;
import kr.co.insoft.core.repository.configration.cdt.CDTMetadataRepository;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Repository
public class CDTMetadataRepositoryImpl implements CDTMetadataRepository {

	private static final Logger CDTMetadataRepositoryImplLog = LoggerFactory
			.getLogger(CDTMetadataRepositoryImpl.class);

	@Autowired
	CDTMetadataEntityMapper cdtMetadataEntityMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.setting.usertype.UserTypeMetadataRepository
	 * #createUserTypeMetadata
	 * (kr.co.insoft.core.model.setting.usertype.UserTypeEntityModel)
	 */
	@Override
	public void createCDTMetadata(CDTEntityModel cdtEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(cdtEntityModel);
			Assert.notNull(cdtEntityModel.getUserTypeId());
			Assert.notEmpty(cdtEntityModel.getRows());
		} catch (IllegalArgumentException e) {
			CDTMetadataRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					cdtEntityModel);
			CDTMetadataRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					cdtEntityModel.getUserTypeId());
			CDTMetadataRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					cdtEntityModel.getRows());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			cdtMetadataEntityMapper.createCDTMetadata(cdtEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.setting.usertype.UserTypeMetadataRepository
	 * #getUserTypeMetadata
	 * (kr.co.insoft.core.model.setting.usertype.UserTypeEntityModel)
	 */
	@Override
	public CDTEntityModel getCDTMetadata(CDTEntityModel cdtEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(cdtEntityModel);
			Assert.notNull(cdtEntityModel.getUserTypeId());
		} catch (IllegalArgumentException e) {
			CDTMetadataRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					cdtEntityModel);
			CDTMetadataRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					cdtEntityModel.getUserTypeId());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			cdtEntityModel.setRows(cdtMetadataEntityMapper
					.getCDTMetadata(cdtEntityModel));
			return cdtEntityModel;
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void removeCDTMetadata(CDTEntityModel cdtEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(cdtEntityModel);
			Assert.notNull(cdtEntityModel.getUserTypeId());
		} catch (IllegalArgumentException e) {
			CDTMetadataRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					cdtEntityModel);
			CDTMetadataRepositoryImplLog.error(ExceptionCode.ASSERT_FORMAT,
					cdtEntityModel.getUserTypeId());
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			cdtMetadataEntityMapper.removeCDTMetadata(cdtEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
}
